﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;
using DBService.ViewDatas;

public partial class Ratings : System.Web.UI.Page, ICallbackEventHandler
{
    ModelAdapter ma = new ModelAdapter();
    string callBackData = String.Empty;
    List<Company> companies;

    protected void Page_Load(object sender, EventArgs e)
    {
        #region callback scripts
        ClientScriptManager cm = Page.ClientScript;
        String cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerData", "");
        String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
        cm.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
        #endregion


        foreach (var c in ma.GetCategoreis(null))
        {
            LitFilterCats.Text += "<div class='sort_box'><a id='" + c.CategoryID + "' class='sff ctss' href='#'>" +
                                      "<span id='Span3' class='uncheked'>&nbsp;</span>" +
                                      "<span class='ch_text'>" + c.Title + "</span>" +
                                  "</a><div class='clear'></div></div>";
        }

        companies = ma.GetCompaniesForFilterPage().OrderByDescending(m => m.Rating).Take(100).ToList();
        BindRep();
    }

    private void BindRep()
    {
        SearchResultCompanies.DataSource = companies;
        SearchResultCompanies.DataBind();
    }

    protected void SearchResultCompanies_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var item = (Company)e.Item.DataItem;
        if (item != null)
        {
            var l = (Literal)e.Item.FindControl("LitRatCalc");
            l.Text = "<div class=\"inner-star\" style=\"width: " + ((int)((item.Rating ?? 0) * 20)).ToString() + "%; margin-left: 1px;\"></div>";
            var l2 = (Literal)e.Item.FindControl("LitRewCount");
            l2.Text = item.Reviews == null ? "0" : item.Reviews.Count.ToString();
        }
    }

    string ICallbackEventHandler.GetCallbackResult()
    {
        return callBackData;
    }

    //private bool Filter(Company c, List<int> cts)
    //{
    //    if (c.CompanyCategories == null)
    //    {
    //        return false;
    //    }
    //    foreach (var cc in c.CompanyCategories)
    //    {
    //        if (cts.Contains(cc.CategoryID))
    //        {
    //            return true;
    //        }
    //    }
    //    return false;
    //}

    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        try
        {
            if (String.IsNullOrEmpty(eventArgument))
            {
                throw new Exception("დაფიქსირდა შეცდომა");
            }
            var parts = eventArgument.Split('|');
            callBackData = "Success|";
            bool mostReviews = Convert.ToBoolean(parts[0]);
            bool highRating = Convert.ToBoolean(parts[1]);
            double ratingFilter = Convert.ToDouble(parts[2]);
            bool isWifi = Convert.ToBoolean(parts[3]);
            bool isOpenAir = Convert.ToBoolean(parts[4]);
            int priceFrom = Convert.ToInt32(parts[5]);
            int priceTo = Convert.ToInt32(parts[6]);

            var filteredList = companies.Where(c =>
                        (c.PriceFrom == null || c.PriceFrom.Value > priceFrom) &&
                        (c.PriceTo == null || c.PriceTo.Value < priceTo) &&
                        (isOpenAir == false || c.OpenAir == true) &&
                        (isWifi == false || c.WiFi == true) &&
                        (c.Rating == null || c.Rating > ratingFilter))
                    .OrderByDescending(co => co.Rating).ToList();
            if (mostReviews)
            {
                filteredList = filteredList.OrderByDescending(co => co.Reviews.Count).ToList();
            }
            int catss = int.Parse(parts[7]);
            if (catss != 0)
            {
                List<int> ct = new List<int>();
                for (int i = 8; i < 8 + catss; i++)
                {
                    ct.Add(int.Parse(parts[i]));
                }

                filteredList = filteredList.Where(co => co.CompanyCategories != null && co.CompanyCategories.Count != 0 &&
                    (co.CompanyCategories.Where(ca => ct.Contains(ca.CategoryID)).FirstOrDefault() != null)).ToList();
            }


            foreach (var item in filteredList)
            {
                callBackData += "<div class=\"comp_block\">" +
                                        "<div class=\"comp_cont\">" +
                                            "<div class=\"result_com_name\">" +
                                                "<a href=\"singlecompany.aspx?id=" + item.CompanyID + " \">" + item.Name + "</a>" +
                                            "</div>" +
                                        "<div class=\"result_vote\">" +
                                            "<span class=\"vote_img\">" +
                                                  "<div class=\"user_rat\">" +
                                                      "<div class=\"outer-star\">" +
                                                          "<div class=\"inner-star\" style=\"width: " + ((int)((item.Rating ?? 0) * 20)).ToString() + "%; margin-left: 1px;\"></div>" +
                                                      "</div>" +
                                                  "</div>" +
                                            "</span>" +
                                            "<span class=\"v_num\">&nbsp;" + (item.Rating ?? 0) + "</span>" +
                                    "</div>" +
                                  "<div class=\"res_addr\">" + item.Address + "&nbsp;&nbsp;&nbsp;" + item.Website + "" +
                                  "</div>" +
                                  "<div class=\"rev_det\">" +
                                      "<span class=\"sum_rev\">" +
                                      "<a href=\"#\">განხილვა(" + (item.Reviews == null ? 0 : item.Reviews.Count) + ")</a>" +
                                       "</span>" +
                                       "<span class=\"det_page\"><a href=\"singlecompany.aspx?id=" + item.CompanyID + "\">დეტალური გვერდი</a> </span>" +
                                  "</div>" +
                                  "</div>" +
                                  "<div class=\"comp_res_img\">" +
                                      "<a href=\"singlecompany.aspx?id=" + item.CompanyID + "\">" +
                                          "<img src=\"upload/" + item.Logo + "\" width=\"104\" height=\"35\" alt=\"Logo\" />" +
                                      "</a>" +
                                  "</div><div class=\"clear\"></div></div>";
            }
        }
        catch (Exception ex)
        {
            callBackData = "Error|" + ex.Message;
        }
    }

}